
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author mathias
 */

public class SocketClient {
    private Socket socketClnt;
    private DataInputStream in;
    private PrintStream out;
    private String host;
    private int port;

    public SocketClient(Socket s, String host, int port){
        socketClnt = s;
        this.host = host;
        this.port = port;
        try {
            socketClnt = new Socket(this.host, this.port);
        } catch (UnknownHostException ex) {
            Logger.getLogger(SocketClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SocketClient.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            in = new DataInputStream(new BufferedInputStream(this.socketClnt.getInputStream()));
        } catch (IOException ex) {
            Logger.getLogger(SocketClient.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            out = new PrintStream(new BufferedOutputStream(this.socketClnt.getOutputStream()));
        } catch (IOException ex) {
            Logger.getLogger(SocketClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean connect(){
        try {
            this.socketClnt.connect(null);
        } catch (IOException ex) {
            Logger.getLogger(SocketClient.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    public boolean disconnect(){
        try {
            this.socketClnt.close();
        } catch (IOException ex) {
            Logger.getLogger(SocketClient.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }


    public void sendData(Message m){
        out.print(m);
        out.flush();
    }

    public Message receiveData(){
        Message m = new Message();
        try {
            System.out.println(in.readLine());
        } catch (IOException ex) {
            Logger.getLogger(SocketClient.class.getName()).log(Level.SEVERE, null, ex);
        }
        return m;
    }

    /*
     * Getters (no useless setters)
     */

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

    public Socket getSocketClnt() {
        return socketClnt;
    }
}
